package com.xzz.service.impl;

import com.xzz.service.ITokenService;
import com.xzz.util.AssertUtil;
import com.xzz.util.StrUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class TokenServiceImpl implements ITokenService {
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    @Override
    public String createToken(String courseId) {
        AssertUtil.isNotNull(courseId,"id不能为空");
        //获得key
        Long loginId=3L;
        String key = "token:"+loginId+":"+courseId;
        //获得value，就是token
        String token = StrUtils.getComplexRandomString(6);
        //加入Redis里面
        redisTemplate.opsForValue().set(key,token,10, TimeUnit.MINUTES);

        return token;
    }
}
